package com.seventh.restaurant.web;

import com.seventh.restaurant.domain.BookingRecord;
import com.seventh.restaurant.domain.Customer;
import com.seventh.restaurant.service.BookingRecordService;
import com.seventh.restaurant.web.util.HeaderUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.hibernate.id.IdentifierGenerator.ENTITY_NAME;

@RestController
@RequestMapping("/api")
public class BookingRecordResource {

    private  final BookingRecordService bookingRecordService;

    public BookingRecordResource(BookingRecordService bookingRecordService) {
        this.bookingRecordService = bookingRecordService;
    }

    @ApiOperation(value="获得一个预约信息")
    @GetMapping("/bookingrecord/one")
    public ResponseEntity<BookingRecord> getOneBookingRecords(@RequestParam Long id) throws Exception {
        BookingRecord bookingRecord = bookingRecordService.getOneBookingRecord(id);
        return ResponseEntity.ok().body(bookingRecord);

    }


    @ApiOperation(value="新增预约信息")
    @PostMapping("/bookingrecord")
    public ResponseEntity<BookingRecord> createBookingRecord(@RequestParam String phonenumber,
                                                             @RequestParam String customername,
                                                     @RequestParam String peoplenum,
                                                     @RequestParam String tableno,
                                                     @RequestParam String arrivaltime,
                                                     @RequestParam String bookingtime,
                                                     @RequestParam String isarrival,
                                                     @RequestParam String isover,
                                                     @RequestParam String recordcancle) throws Exception {
        if (phonenumber!=null && customername!=null && peoplenum!=null &&tableno!=null && arrivaltime!=null && bookingtime!=null && isarrival!=null &&isover!=null && recordcancle!=null){
            BookingRecord bookingRecord = new BookingRecord();
            Customer customer = new Customer();
            customer.setName(customername);
            customer.setPhoneNumber(phonenumber);
            bookingRecord.setCustomer(customer);
            bookingRecord.setPeopleNum(peoplenum);
            bookingRecord.setTableNo(tableno);
            bookingRecord.setArrivalTime(arrivaltime);
            bookingRecord.setBookingTime(bookingtime);
            bookingRecord.setIsArrival(isarrival);
            bookingRecord.setIsOver(isover);
            bookingRecord.setRecordCancle(recordcancle);
            BookingRecord bookingRecord1 = bookingRecordService.insertBookingRecord(bookingRecord);
            return ResponseEntity.ok().body(bookingRecord1);
        }else {
            return new ResponseEntity<BookingRecord>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="修改预约信息")
    @PutMapping("/bookingrecord")
    public ResponseEntity<BookingRecord> updateBookingRecord(@RequestParam Long id,
                                                     @RequestParam String peoplenum,
                                                     @RequestParam String tableno,
                                                     @RequestParam String arrivaltime,
                                                     @RequestParam String isarrival,
                                                     @RequestParam String isover,
                                                     @RequestParam String recordcancle) throws Exception {
        if (peoplenum!=null &&tableno!=null && arrivaltime!=null  && isarrival!=null &&isover!=null && recordcancle!=null){
            BookingRecord bookingRecord = bookingRecordService.updateBookingRecord(id,peoplenum,tableno,arrivaltime,isarrival,isover,recordcancle);
            return ResponseEntity.ok().body(bookingRecord);
        }else {
            return new ResponseEntity<BookingRecord>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="取消预约")
    @PutMapping("/bookingrecord/cancel")
    public ResponseEntity<BookingRecord> cancelBookingRecord(@RequestParam Long id) throws Exception {
        if (id!=null ){
         BookingRecord bookingRecord = bookingRecordService.cancleBookingRecord(id);
            return ResponseEntity.ok()
                    .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, bookingRecord.getId().toString()))
                    .body(bookingRecord);
        }else {
            return new ResponseEntity<BookingRecord>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="结束用餐")
    @PutMapping("/bookingrecord/end")
    public ResponseEntity<BookingRecord> endBookingRecord(@RequestParam Long id) throws Exception {
        if (id!=null ){
            BookingRecord bookingRecord = bookingRecordService.endBookingRecord(id);
            return ResponseEntity.ok()
                    .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, bookingRecord.getId().toString()))
                    .body(bookingRecord);
        }else {
            return new ResponseEntity<BookingRecord>(HttpStatus.BAD_REQUEST);
        }
    }
    @ApiOperation(value="开始用餐")
    @PutMapping("/bookingrecord/start")
    public ResponseEntity<BookingRecord> startBookingRecord(@RequestParam Long id) throws Exception {
        if (id!=null ){
            BookingRecord bookingRecord = bookingRecordService.startBookingRecord(id);
            return ResponseEntity.ok()
                    .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, bookingRecord.getId().toString()))
                    .body(bookingRecord);
        }else {
            return new ResponseEntity<BookingRecord>(HttpStatus.BAD_REQUEST);
        }
    }


    @ApiOperation(value="获得预约信息List")
    @GetMapping("/bookingrecord/alllist")
    public ResponseEntity<List<BookingRecord>> getAllBookingRecords(@RequestParam String kind) {
        List<BookingRecord> bookingRecords = bookingRecordService.findALLBookingRecords(kind);
        return new ResponseEntity<>(bookingRecords, HttpStatus.OK);
    }

    @ApiOperation(value="根据手机号获得预约信息List")
    @GetMapping("/bookingrecord/phonenumberlist")
    public ResponseEntity<List<BookingRecord>> getPhonenumberBookingRecords(@RequestParam String phonenumber) {
        List<BookingRecord> bookingRecords = bookingRecordService.findBookingRecord(phonenumber);
        return new ResponseEntity<>(bookingRecords, HttpStatus.OK);
    }
}
